<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import type { FormInstance, FormRules } from 'element-plus'
import userApi from '@/api/user'

const router = useRouter()

const loginForm = reactive({
  username: '',
  password: ''
})

const loginFormRef = ref<FormInstance>()

const rules = reactive<FormRules>({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应在3-20个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度应在6-20个字符之间', trigger: 'blur' }
  ]
})

const handleLogin = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid) => {
    if (valid) {
      try {
        const res = await userApi.login(loginForm)
        ElMessage.success('登录成功')
        localStorage.setItem('token', res.data.token)
        localStorage.setItem('userInfo', JSON.stringify(res.data.user))
        router.push('/')
      } catch (error) {
        ElMessage.error('登录失败，请稍后重试')
      }
    }
  })
}

const goToRegister = () => {
  router.push('/register')
}
</script>

<template>
  <div class="login-container">
    <div class="login-content">
      <div class="login-left">
        <div class="system-info">
          <h1 class="system-title">仓库管理系统</h1>
          <p class="system-desc">高效、专业的仓库管理解决方案</p>
        </div>
      </div>
      <div class="login-right">
        <el-card class="login-card">
          <template #header>
            <h2 class="login-title">用户登录</h2>
          </template>
          <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="rules"
            label-width="80px"
            style="width: 100%"
            status-icon
          >
            <el-form-item label="用户名" prop="username">
              <el-input
                v-model="loginForm.username"
                :prefix-icon="User"
                placeholder="请输入用户名"
                clearable
              />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input
                v-model="loginForm.password"
                :prefix-icon="Lock"
                type="password"
                placeholder="请输入密码"
                show-password
                clearable
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" class="login-button" @click="handleLogin(loginFormRef)">
                登录
              </el-button>
            </el-form-item>
            <div class="register-link">
              <span>没有账号？</span>
              <el-button link type="primary" @click="goToRegister">立即注册</el-button>
            </div>
          </el-form>
        </el-card>
      </div>
    </div>
  </div>
</template>

<style scoped>

.login-container {
  height: 100%;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f0f2f5;
}

.login-content {
  display: flex;
  width: 100%;
  height: 100%;
  background: white;
  border-radius: 12px;
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.15);
  overflow: hidden;
}

.login-left {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #1890ff 0%, #52c41a 100%);
  border-radius: 8px 0 0 8px;
  padding: 40px;
}

.system-info {
  color: white;
  text-align: center;
}

.system-title {
  font-size: 42px;
  font-weight: 600;
  margin-bottom: 24px;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
}

.system-desc {
  font-size: 20px;
  opacity: 0.9;
  line-height: 1.5;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
}

.login-right {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.login-card {
  width: 85%;
  max-width: 400px;
  border: none;
  box-shadow: none;
}

.login-title {
  text-align: center;
  margin: 0 0 20px 0;
  color: #1890ff;
  font-size: 24px;
  font-weight: 600;
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 2px;
  margin-top: 20px;
  border-radius: 22px;
  background: linear-gradient(135deg, #1890ff 0%, #52c41a 100%);
  border: none;
  transition: all 0.3s ease;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.register-link {
  text-align: center;
  margin-top: 20px;
  color: #606266;
}
</style>